﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.orderLinqDAL;
using dhDataAcess.dataAcess.purchaseTableAdapters;
using dhDataAcess.dataAcess.baobiaoDataSetTableAdapters;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.orderDAL;
using dhDataAcess.produceLinq;
using dhDataAcess.produceDAL;
using dhDataAcess.sysDAL;

namespace dhSystemBLL.produceBLL
{
   public class rltkBLL
    {
       private V_rltkDetailTableAdapter _V_rltkDetailAdapter = null;
       private V_rltkDetailTableAdapter V_rltkDetailAdapter
        {
            get
            {
                if (_V_rltkDetailAdapter == null) _V_rltkDetailAdapter = new V_rltkDetailTableAdapter();
                return _V_rltkDetailAdapter;

            }
        }
        private rltkMasterTableAdapter _rltkMasterlAdapter = null;
        private rltkMasterTableAdapter rltkMasterAdapter
        {
            get
            {
                if (_rltkMasterlAdapter == null) _rltkMasterlAdapter = new rltkMasterTableAdapter();
                return _rltkMasterlAdapter;

            }
        }
        private rltkDetailTableAdapter _rltkDetailAdapter = null;
        private rltkDetailTableAdapter rltkDetailAdapter
        {
            get
            {
                if (_rltkDetailAdapter == null) _rltkDetailAdapter = new rltkDetailTableAdapter();
                return _rltkDetailAdapter;

            }
        }
        /// <summary>
        /// 查询领料退库主表
        /// </summary>
        /// <returns></returns>

        public purchase.rltkMasterDataTable getrltkMasterAll()
        {
            try
            {
                return rltkMasterAdapter.GetDatarltkMasterALL();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 查询领料退库从表
        /// </summary>
        /// <returns></returns>

        public purchase.rltkDetailDataTable getrltkDetailAll()
        {
            try
            {
                return rltkDetailAdapter.GetDatarltkDetailALL();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 查询领料退库从表
        /// </summary>
        /// <returns></returns>

        public baobiaoDataSet.V_rltkDetailDataTable getV_rltkDetailBystockNamelikeNo(string matName)
        {
            try
            {
                return V_rltkDetailAdapter.GetDataV_rltkDetailBystockNamelikeNo(matName);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 查询领料退库从表
        /// </summary>
        /// <returns></returns>

        public baobiaoDataSet.V_rltkDetailDataTable getV_rltkDetailBystockNamelike(string matName)
        {
            try
            {
                return V_rltkDetailAdapter.GetDataV_rltkDetailBystockNamelike (matName);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 查询领料退库从表
        /// </summary>
        /// <returns></returns>

        public baobiaoDataSet.V_rltkDetailDataTable getV_rltkDetailBystockNameNo()
        {
            try
            {
                return V_rltkDetailAdapter.GetDataV_rltkDetailBystockNameNo ();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 查询领料退库从表
        /// </summary>
        /// <returns></returns>

        public baobiaoDataSet.V_rltkDetailDataTable getV_rltkDetailBystockName()
        {
            try
            {
                return V_rltkDetailAdapter.GetDataV_rltkDetailBystockname ();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 根据入库单号查询领料退库表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
        public purchase.rltkMasterDataTable getrltkMasterAllBybillNo(string billNo)
        {
            try
            {
                return rltkMasterAdapter.GetDatarltkMasterBybillNo(billNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 根据入库单号查询领料退库从表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
        public purchase.rltkDetailDataTable getsrltkDetailAllBybillNo(string billNo)
        {
            try
            {
                return rltkDetailAdapter.GetDatarltkDetailBybillNo(billNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 修改领料退库表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int rltkInsert(purchase.rltkMasterDataTable masterTable, purchase.rltkDetailDataTable pTable, string state, string billNo, string Date)
        {
            rltkDetailModel rltkDetailRow = new rltkDetailModel();
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                rltkMasterDAL rltkMaster = new rltkMasterDAL(tn, conn);
                rltkDetailDAL rltkDetail = new rltkDetailDAL(tn, conn);
                rltkMaster.rltkMasterInsertII(masterTable, pTable);
                //basListDAL basList = new basListDAL(tn, conn);
                //if (state == "新增")
                //{

                //    purchase.rltkMasterDataTable rltkTable = getrltkMasterAllBybillNo(billNo);
                //    if (rltkTable.Rows.Count == 0)
                //    {
                //        rltkMaster.rltkMasterInsert(row);
                //    }
                //    else
                //    {
                //        row.billNo = "R" + Date + "-" + basicSql.getOrderSerail("领料退库单号");
                //        rltkMaster.rltkMasterInsert(row);
                //    }

                //    //更新单据编号的过程 
                //    string orderSerial = basicSql.getOrderSerail("领料退库单号");
                //    basList.basListretbasNumUpdate((Convert.ToInt32(orderSerial) + 1).ToString(), "领料退库单号");
                //}
                //if (state == "编辑")
                //{
                //    rltkMaster.rltkMasterUpdate(row);
                //}


                //foreach (purchase.rltkDetailRow detailRow in pTable)
                //{
                //    if (detailRow.RowState != DataRowState.Deleted)
                //    {
                //        rltkDetailRow.recordid = detailRow.recordid;
                //        rltkDetailRow.billNo = detailRow.billNo;
                //        rltkDetailRow.matNum = detailRow.matNum;
                //        rltkDetailRow.matName = detailRow.matName ;
                //        if (!detailRow.IsmatSpecNull ())
                //        {
                //            rltkDetailRow.matSpec = detailRow.matSpec;
                //        }
                //        else
                //        {
                //            rltkDetailRow.matSpec = "";
                //        }
                //        if (!detailRow.IsqtyNull())
                //        {
                //            rltkDetailRow.qty = detailRow.qty;
                //        }
                //        else { rltkDetailRow.qty = 0; }
                //        if (!detailRow.IsfqtyNull ())
                //        {
                //            rltkDetailRow.fqty = detailRow.fqty;
                //        }
                //        else { rltkDetailRow.fqty = 0; }
                //        if (!detailRow.IsfqtyNull())
                //        {
                //            rltkDetailRow.funit = detailRow.funit;
                //        }
                //        else { rltkDetailRow.funit  = ""; }
                //        if (!detailRow.IsunitNull ())
                //        {
                //            rltkDetailRow.unit = detailRow.unit;
                //        }
                //        else { rltkDetailRow.unit = ""; }
                //        rltkDetailRow.createDate = DateTime.Now;

                //        if (!detailRow.IsmarkNull())
                //        {
                //            rltkDetailRow.mark = detailRow.mark;
                //        }
                //        else
                //        {
                //            rltkDetailRow.mark = "";
                //        }
                //    }

                //    //编辑子表记录,新增
                //    if (detailRow.RowState == DataRowState.Added)
                //    {

                //        rltkDetail.rltkDetailInsert(rltkDetailRow);


                //    }
                //    //修改
                //    if (detailRow.RowState == DataRowState.Modified)
                //    {
                //        rltkDetail.rltkDetailUpdate (rltkDetailRow);
                //    }
                //    //删除
                //    if (detailRow.RowState == DataRowState.Deleted)
                //    {
                //        DataRow delrow = (DataRow)detailRow;
                //        rltkDetail.rltkDetailDel(delrow["recordid", DataRowVersion.Original].ToString());
                //    }


                //}

                tn.Commit();
                return i;

            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个入库单号已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }


        /// 删除一行领料退库表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int rltkMasterDel(string billNo)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                rltkMasterDAL rltkMaster = new rltkMasterDAL(tn, conn);
                rltkMaster.rltkDel(billNo);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }
    }
}
